import javax.swing._
import java.awt._
import java.awt.event.ActionEvent

class GradeConverter extends JFrame {
  // 先声明组件变量
  private val scoreField: JTextField = new JTextField()
  private val resultLabel: JLabel = new JLabel("", SwingConstants.CENTER)
  private val commentLabel: JLabel = new JLabel("", SwingConstants.CENTER)
  private val convertButton: JButton = new JButton("转换！")
  private val colorTimer: Timer = new Timer(300, _ => convertButton.setBackground(new Color(135, 206, 250)))

  // 构造方法
  {
    setTitle("成绩等级转换器")
    setSize(400, 300)
    setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE)
    setLayout(new GridLayout(5, 1, 10, 10))
    setLocationRelativeTo(null)

    // 标题
    val titleLabel = new JLabel("输入你的分数（0-100）", SwingConstants.CENTER)
    titleLabel.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 18))

    // 输入框
    scoreField.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 16))
    scoreField.setHorizontalAlignment(SwingConstants.CENTER)

    // 按钮
    convertButton.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 16))
    convertButton.setBackground(new Color(135, 206, 250))

    // 结果与评语
    resultLabel.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 20))
    commentLabel.setFont(new Font(Font.SANS_SERIF, Font.ITALIC, 14))

    // 添加组件
    add(titleLabel)
    add(scoreField)
    add(convertButton)
    add(resultLabel)
    add(commentLabel)

    // 按钮点击事件
    convertButton.addActionListener(_ => convertGrade())

    // 初始化定时器
    colorTimer.setRepeats(false)
  }

  private def convertGrade(): Unit = {
    val input = scoreField.getText.trim
    if (input.isEmpty) {
      JOptionPane.showMessageDialog(this, "请输入分数后再转换！", "提示", JOptionPane.WARNING_MESSAGE)
      return
    }

    try {
      val score = input.toDouble
      if (score < 0 || score > 100) {
        JOptionPane.showMessageDialog(this, "请输入0-100之间的分数！", "错误", JOptionPane.ERROR_MESSAGE)
        return
      }

      // 按钮变色
      convertButton.setBackground(Color.PINK)
      if (colorTimer.isRunning) colorTimer.stop()
      colorTimer.start()

      // 等级判断
      val (grade, comment) =
        if (score >= 90) ("A", "太棒了！继续保持！")
        else if (score >= 80) ("B", "很好！再接再厉！")
        else if (score >= 70) ("C", "不错，但还有提升空间！")
        else if (score >= 60) ("D", "刚好及格，下次要更努力哦！")
        else ("F", "别灰心，加油！你可以做到的！")

      // 显示结果
      resultLabel.setText(s"等级: $grade")
      commentLabel.setText(comment)

      // 设置颜色
      grade match {
        case "A" => resultLabel.setForeground(Color.RED)
        case "B" => resultLabel.setForeground(Color.BLUE)
        case "C" => resultLabel.setForeground(Color.GREEN)
        case "D" => resultLabel.setForeground(Color.ORANGE)
        case "F" => resultLabel.setForeground(Color.GRAY)
      }

      // 清空输入框并聚焦
      scoreField.setText("")
      scoreField.requestFocus()

    } catch {
      case _: NumberFormatException =>
        JOptionPane.showMessageDialog(this, "请输入有效的数字！", "错误", JOptionPane.ERROR_MESSAGE)
    }
  }
}

object GradeConverterApp {
  def main(args: Array[String]): Unit = {
    SwingUtilities.invokeLater(() => {
      new GradeConverter().setVisible(true)
    })
  }
}